/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test_app;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;


import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author assa
 */
public class Yandex_helper {

   private Document doc;
   private String YndexURL = "http://yandex.ru/yandsearch?";
   private String Search_str = "digital агенство";
   private String Region = "191"; //http://noindex.by/index/lr-v-yandekse
    
    
    public Yandex_helper() {
        get_page();
    }

    public Yandex_helper(String Search_str, String Region) {
        this.Region = Region;
        this.Search_str = Search_str;
        get_page();
    }
    
    public void get_page(){
          System.out.println("Get page");
       try {
            doc = Jsoup.connect(get_link()).get();
        } catch (IOException ex) {
            Logger.getLogger(Yandex_helper.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Not connect");
        }
    }
            
private String get_link() throws IOException{

  
Search_str = URLEncoder.encode(Search_str, "UTF-8");

    StringBuilder address = new StringBuilder(YndexURL);
             address.append("text=").append(Search_str).append("&");
             address.append("lr=").append(Region).append("&");

             return address.toString();
 }

public ArrayList<String> get_links(){
 
         Elements content = doc.getElementsByClass("b-serp-item__title-link");
         ArrayList<String> links = new ArrayList<String>();
         for (Element link : content) {
            links.add(link.attr("href"));
        }
         return links;
 }

public ArrayList<String> get_text(){
 
         Elements content = doc.getElementsByClass("b-serp-item__title-link");
         ArrayList<String> texts = new ArrayList<String>();
         for (Element text : content) {
            texts.add(text.text());
        }
         return texts;
 }


}
